package com.zjht.gmcc.bean.action;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.zjht.gmcc.bean.model.Workflow;
import com.zjht.gmcc.bean.util.MyParam;

/**
 * 工作流对象相关方法
 * @author dyh 2010-06-10
 */
public class WorkflowUtil implements java.io.Serializable {
	private static final long serialVersionUID = 2958898838657051800L;

	// 业务流状态
	public static final String STATUS_START = "0";// 开始状态
	public static final String STATUS_END = "99";// 结束状态
	public static final String STATUS_STOP = "-1";// 非正常结束状态(终止)
	public static final String STATUS_NULL = "-2";// 非法状态

	public static final int NODEID_END = 99;// 流程结束节点
	public static final int NODEID_STOP = -1;// 非正常结束节点
	public static final int NODEID_NULL = -2;// 空节点

	// 流程业务类型
	public static final String FLOWTYPE_CHOICE = "1";// 选型
	public static final String FLOWTYPE_RGSCHOICE = "2";// 入柜商选型

	/**
	 * 输入节点ID，输出工作流对象
	 * @author dyh 2010-06-10
	 * @param nodeid 节点ID
	 * @return 工作流对象
	 */
	public static Workflow getWorkflowByNodeID(String flowtype, int nodeid) {
		Workflow wf = null;
		LinkedHashMap<Integer, Workflow> tmp = MyParam.hWorkflow.get(flowtype);
		if (tmp != null)
			wf = tmp.get(Integer.valueOf(nodeid));
		return wf;
	}

	/**
	 * 输入节点ID，输出节点名称
	 * @author dyh 2010-06-10
	 * @param nodeid 节点ID
	 * @return 节点名称(如果异常，则返回"")
	 */
	public static String getNodeName(String flowtype, int nodeid) {
		if (nodeid == NODEID_END)
			return "流程结束";
		else if (nodeid == NODEID_STOP)
			return "流程中止";
		else {
			Workflow wf = getWorkflowByNodeID(flowtype, nodeid);
			return wf == null ? "" : wf.getNodename();
		}
	}

	/**
	 * 输入节点ID，输出前置节点集合(用于绑定下拉框)
	 * @author dyh 2010-06-10
	 * @param nodeid 节点ID
	 * @return 前置节点集合(如果异常，则返回null)
	 */
	public static Map<String, String> getForwadNodes(String flowtype, int nodeid) {
		Workflow wf = getWorkflowByNodeID(flowtype, nodeid);
		String forwardnode = wf.getForwardnode();
		Map<String, String> nodes = null;
		if (forwardnode != null && forwardnode.length() > 0) {
			nodes = new LinkedHashMap<String, String>();
			String[] forwardnodes = forwardnode.split(",");
			String nodename = "";
			String nodeid_end = String.valueOf(NODEID_END);
			String nodeid_stop = String.valueOf(NODEID_STOP);
			for (String node : forwardnodes) {
				if (nodeid_end.equals(node))
					nodename = "结束流程";
				else if (nodeid_stop.equals(node))
					nodename = "终止流程";
				else
					nodename = "送" + getNodeName(flowtype, Integer.parseInt(node));
				nodes.put(node, nodename);
			}
		}
		return nodes;
	}

	/**
	 * 输入节点ID，输出后退节点集合(用于绑定下拉框)
	 * @author dyh 2010-06-10
	 * @param nodeid 节点ID
	 * @return 后退节点集合(如果异常，则返回null)
	 */
	public static Map<String, String> getBackwadNodes(String flowtype, int nodeid) {
		Workflow wf = getWorkflowByNodeID(flowtype, nodeid);
		String backwardnode = wf.getBackwardnode();
		Map<String, String> nodes = null;
		if (backwardnode != null && backwardnode.length() > 0) {
			nodes = new LinkedHashMap<String, String>();
			String[] backwardnodes = backwardnode.split(",");
			String nodename = "";
			String nodeid_end = String.valueOf(NODEID_END);
			String nodeid_stop = String.valueOf(NODEID_STOP);
			for (String node : backwardnodes) {
				if (nodeid_end.equals(node))
					nodename = "结束流程";
				else if (nodeid_stop.equals(node))
					nodename = "终止流程";
				else
					nodename = "退回" + getNodeName(flowtype, Integer.parseInt(node));
				nodes.put("@" + node, nodename);
			}
		}
		return nodes;
	}

	/**
	 * 输入节点ID，输出所有节点集合(用于绑定下拉框)
	 * @author dyh 2010-06-10
	 * @param nodeid 节点ID
	 * @return 所有节点集合(如果异常，则返回null)
	 */
	public static Map<String, String> getAllNodes(String flowtype, int nodeid) {
		Map<String, String> allnodes = null;
		Map<String, String> nodes = getForwadNodes(flowtype, nodeid);
		if (nodes != null) {
			if (allnodes == null)
				allnodes = new LinkedHashMap<String, String>();
			allnodes.putAll(nodes);
		}
		nodes = getBackwadNodes(flowtype, nodeid);
		if (nodes != null) {
			if (allnodes == null)
				allnodes = new LinkedHashMap<String, String>();
			allnodes.putAll(nodes);
		}
		return allnodes;
	}

	/**
	 * 获取满足status的最小的当前节点ID
	 * @author dyh 2010-06-10
	 * @param flowtype 业务类型
	 * @param nodeid 节点ID
	 * @return 最小的当前节点ID(如果异常，返回NODEID_NULL)
	 */
	public static int getCurrNode(String flowtype, String status) {
		Map<Integer, Workflow> nodes = MyParam.hWorkflow.get(flowtype);
		int nodeid = NODEID_NULL;
		if (nodes != null && nodes.size() > 0) {
			Iterator<Entry<Integer, Workflow>> iter = nodes.entrySet().iterator();
			Entry<Integer, Workflow> entry = null;
			Workflow node = null;
			while (iter.hasNext()) {
				entry = iter.next();
				node = entry.getValue();
				if (status.equals(node.getStatus())) {
					nodeid = node.getNodeid();
					break;
				}
			}
		}
		return nodeid;
	}

	/**
	 * 获取满足status的最小的下一节点ID
	 * @author dyh 2010-06-10
	 * @param flowtype 业务类型
	 * @param nodeid 节点ID
	 * @return 最小的下一节点ID(如果异常，返回NODEID_NULL)
	 */
	public static int getNextNode(String flowtype, String status) {
		Map<Integer, Workflow> nodes = MyParam.hWorkflow.get(flowtype);
		int nodeid = NODEID_NULL;
		if (nodes != null && nodes.size() > 0) {
			String forwardnode = "";
			Iterator<Entry<Integer, Workflow>> iter = nodes.entrySet().iterator();
			Entry<Integer, Workflow> entry = null;
			Workflow node = null;
			while (iter.hasNext()) {
				entry = iter.next();
				node = entry.getValue();
				if (status.equals(node.getStatus())) {
					forwardnode = node.getForwardnode();
					if (forwardnode != null) {
						if (forwardnode.indexOf(",") != -1) {
							forwardnode = forwardnode.substring(0, forwardnode.indexOf(",") - 1);
						}
						try {
							nodeid = Integer.parseInt(forwardnode);
						} catch (Exception x) {
						}
					}
					break;
				}
			}
		}
		return nodeid;
	}

	/**
	 * 根据角色和状态，获取节点ID
	 * @author dyh 2010-06-10
	 * @param flowtype 业务类型
	 * @param dutyid 角色
	 * @param status 状态
	 * @return 节点ID(如果异常，则返回NODEID_NULL)
	 */
	public static int getCurrNode(String flowtype, String status, String dutyid) {
		if (dutyid == null || dutyid.length() == 0)
			return getCurrNode(flowtype, status);
		Map<Integer, Workflow> nodes = MyParam.hWorkflow.get(flowtype);
		int nodeid = NODEID_NULL;
		if (nodes != null && nodes.size() > 0) {
			String duty = "";
			Iterator<Entry<Integer, Workflow>> iter = nodes.entrySet().iterator();
			Entry<Integer, Workflow> entry = null;
			Workflow node = null;
			while (iter.hasNext()) {
				entry = iter.next();
				node = entry.getValue();
				if (status.equals(node.getStatus())) {
					duty = node.getNoderole();
					if (duty != null && duty.indexOf("@" + dutyid) != -1) {
						nodeid = node.getNodeid();
						break;
					}
				}
			}
		}
		if (nodeid == NODEID_NULL) {
			nodeid = getCurrNode(flowtype, status);
		}
		return nodeid;
	}

	/**
	 * 输入两节点ID，输出当前流程状态(待处理节点)
	 * @author dyh 2010-06-10
	 * @param currnode 当前已处理节点
	 * @param nextnode 待处理节点
	 * @return 当前流程状态(待处理节点)(如果异常，则返回"")
	 */
	public static String getNodeName(String flowtype, int currnode, int nextnode) {
		String status = "";
		if (nextnode == NODEID_END) {
			status = "<b>流程结束</b>";
		} else if (nextnode == NODEID_STOP) {
			status = "<font color='red'>流程终止</font>";
		} else if (currnode > nextnode) {
			status = "<font color='red'>退回" + getNodeName(flowtype, nextnode) + "</font>";
		} else {
			status = getNodeName(flowtype, nextnode);
		}
		return status;
	}

	/**
	 * 输入节点ID，判断是否为退回节点(用@标记)
	 * @author dyh 2010-06-10
	 * @param nodeid 节点
	 * @return 是否为退回节点(用@标记)
	 */
	public static boolean isBackwardNode(String nodeid) {
		return nodeid != null && nodeid.indexOf("@") != -1;
	}

	/**
	 * 输入节点ID，输出节点状态
	 * @author dyh 2010-06-10
	 * @param nodeid 节点ID
	 * @return 节点状态(如果异常，则返回"")
	 */
	public static String getNodeStatus(String flowtype, int nodeid) {
		if (nodeid == NODEID_END) {
			return STATUS_END;
		} else if (nodeid == NODEID_STOP) {
			return STATUS_STOP;
		} else {
			Workflow wf = getWorkflowByNodeID(flowtype, nodeid);
			return wf == null ? "" : wf.getStatus();
		}
	}

	/**
	 * 根据角色和状态，判断节点ID是否为相同状态中第一个节点
	 * @author dyh 2010-06-10
	 * @param flowtype 业务类型
	 * @param status 状态
	 * @param node 待检测的节点
	 * @return 是否为相同状态中第一个节点
	 */
	public static boolean isFirstNode(String flowtype, String status, int node) {
		return getCurrNode(flowtype, status) == node;
	}

	/**
	 * 根据角色和状态，判断节点ID是否为相同状态中最后一个节点
	 * @author dyh 2010-06-10
	 * @param flowtype 业务类型
	 * @param status 状态
	 * @param nodeid 待检测的节点
	 * @return 是否为相同状态中最后一个节点
	 */
	public static boolean isLastNode(String flowtype, String status, int nodeid) {
		Map<Integer, Workflow> nodes = MyParam.hWorkflow.get(flowtype);
		int nodeidOK = NODEID_NULL;
		boolean hasSameStatus = false;
		if (nodes != null && nodes.size() > 0) {
			Iterator<Entry<Integer, Workflow>> iter = nodes.entrySet().iterator();
			Entry<Integer, Workflow> entry = null;
			Workflow node = null;
			while (iter.hasNext()) {
				entry = iter.next();
				node = entry.getValue();
				if (status.equals(node.getStatus())) {
					if (nodeidOK == NODEID_NULL)
						hasSameStatus = true;
					nodeidOK = node.getNodeid();
				} else {
					if (hasSameStatus)
						break;// 当进入下一个不同状态时，退出遍历
				}
			}
		}
		return nodeidOK == nodeid;
	}

	/**
	 * 输入节点ID，判断是否为流程结束或终止节点
	 * @author dyh 2010-06-10
	 * @param nodeid 节点
	 * @return 是否为流程结束或终止节点
	 */
	public static boolean isEndNode(int nodeid) {
		return nodeid == NODEID_END || nodeid == NODEID_STOP;
	}
}
